﻿using STHPMS1.DAL.D_Core;
using System.Data;
using ServiceStack.OrmLite;
using STHPMS1.ICore.I_Entity.Stmpms1;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore.I_DAL.Repository;
using System.Collections.Generic;


namespace STHPMS1.DAL.D_Repository
{
    public class E_SegevaluRepository : DalBase, IE_SegevaluRepository
    {
        /// <summary>
        /// 更新路段评定中不同指标的值
        /// </summary>
        /// <param name="inspecid">评定名称</param>
        /// <param name="segid">基础路段ID</param>
        /// <param name="rdwayid">路线编号</param>
        /// <param name="type">指标</param>
        /// <param name="value">指标值</param>
        public void Update(string inspecid, string segid, string rdwayid, string type, double value)
        {
            IDbConnection conn = Adapter.OpenDbConnection();
            conn.ExecuteNonQuery("update E_SEGEVALU set " + type + "=" + value + " where INSPECID='" + inspecid + "' and SEGID='" + segid + "' and RDWAYID='" + rdwayid + "'");
           
        }


        /// <summary>
        /// 当前路线下所有路段某个指标的平均值
        /// </summary>
        /// <param name="inspecid">评定名称</param>
        /// <param name="rdwayid">路线编号</param>
        /// <param name="Type">指标</param>
        /// <returns></returns>
        public Dictionary<string, double> GetSegidValue(string inspecid, string rdwayid, string Type)
        {
            IDbConnection conn = Adapter.OpenDbConnection();
            return conn.Dictionary<string, double>("select B_INVNTORY.SEGID,SUM(" + Type + "*(B_UNIT.ENDMP-B_UNIT.BEGINMP))/SUM(B_UNIT.ENDMP-B_UNIT.BEGINMP) as " + Type + " from E_ROADWAYDETAIL "
                                                    + "left join B_UNIT on E_ROADWAYDETAIL.UNIT=B_UNIT.UNIT  "
                                                    + "left join B_INVNTORY_UNIT on E_ROADWAYDETAIL.UNIT=B_INVNTORY_UNIT.UNIT "
                                                    + "left join B_INVNTORY  on B_INVNTORY_UNIT.SEGID=B_INVNTORY.SEGID where B_INVNTORY.RDWAYID='" + rdwayid + "' and " + Type + " is not null and INSPECID='" + inspecid + "'  "
                                                    + "group by B_INVNTORY.SEGID");
        }

    }
}
